﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml;

namespace MindmeisterAPI
{
    class RequestTools
    {

        public static XmlDocument ExecuteRequest(string pEndpoint, string pApiKey, Request pRequest)
        {
            XmlDocument xml = new XmlDocument();


            // Create the web request   
            HttpWebRequest request = WebRequest.Create(GetUrl(pEndpoint, pApiKey, pRequest)) as HttpWebRequest;

            // Get response   
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                // Get the response stream   
                StreamReader reader = new StreamReader(response.GetResponseStream());

                // Console application output   
                xml.LoadXml(reader.ReadToEnd());
            }

            return xml;

        }
        public static string GetUrl(string pEndpoint, string pApiKey, Request pRequest)
        {
            return string.Format("{0}?{1}", pEndpoint, pRequest.GetParamsUrl());
        }
    }
}
